/**
 * 功能:TK_GOODS Dao类
 * 开发人员:yaren
 * 创建时间:2012/4/21 23:42:37
 */
package com.towker.frame.dao.basicData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.towker.core.db.BaseDao;
import com.towker.frame.model.basicData.Goods;

@Repository
@SuppressWarnings("unchecked")
public class GoodsDao extends BaseDao<Goods, Long>
{
	@Override
	public Class getEntityClass()
	{
		return Goods.class;
	}
	public int batchUpdJprice(Integer typeId, Integer classId, String goodsSn1, String goodsSn2, double jprice, double tprice_value, double bprice_value, double wprice_value, double sprice_value){
		final Map<String, Object> map = new HashMap<String, Object>();
		if (typeId != null) {
			map.put("type_id", typeId);
		}
		if (classId != null) {
			map.put("class_id", classId);
		}
		if (StringUtils.isNotBlank(goodsSn1)) {
			map.put("goods_sn1", goodsSn1);
		}
		if (StringUtils.isNotBlank(goodsSn2)) {
			map.put("goods_sn2", goodsSn2);
		}
		map.put("jprice_value", jprice);
		map.put("tprice_value", tprice_value);
		map.put("bprice_value", bprice_value);
		map.put("wprice_value", wprice_value);
		map.put("sprice_value", sprice_value);
		
		return (Integer) getSqlSessionTemplate().execute(new SqlSessionCallback() {
			
			@Override
			public Object doInSession(SqlSession session) {
				return session.update(getIbatisMapperNamespace() + ".batchUpdJprice", map);
			}
		});
	}
	public boolean checkExistsCode(String goodsSn, Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("goods_sn", goodsSn);
		if (id != null & id > 0) {
			map.put("id", id);
		}
		Integer totalCount = (Integer) this.getSqlSessionTemplate().selectOne(getIbatisMapperNamespace() + ".checkExistsCode", map);
		if (totalCount == null) {
			return false;
		} else {
			return totalCount > 0;
		}
	}
	public Goods getByGoodsSn(String goodsSn) {
		String statementName = getIbatisMapperNamespace() + ".getByGoodsSn";
		Map params = new TreeMap<String, Object>();
		params.put("goods_sn", goodsSn);
		List<Goods> goods = getSqlSessionTemplate().selectList(statementName, params);
		if (goods.size() > 0) {
			return goods.get(0);
		}
		return null;
	}
}